package com.server.base.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.server.common.constants.RedisKeyConstant;
import com.server.common.constants.TokenConstant;
import com.server.common.exception.BusinessException;
import com.server.common.utils.JwtTokenUtil;
import com.server.common.utils.RedisUtil;
import com.server.base.dto.AdminLoginDto;
import com.server.base.entity.SysAdmin;
import com.server.base.entity.SysMenu;
import com.server.base.entity.SysRole;
import com.server.base.mapper.SysAdminMapper;
import com.server.base.service.ISysAdminService;
import com.server.base.service.ISysMenuService;
import com.server.base.service.ISysRoleService;
import com.server.base.vo.AdminLoginSuccessVO;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author zgr
 * @since 2023-07-11
 */
@Service
public class SysAdminServiceImpl extends ServiceImpl<SysAdminMapper, SysAdmin> implements ISysAdminService {

    private final RedisUtil redisUtil;
    private final ISysRoleService sysRoleService;
    private final ISysMenuService sysMenuService;

    public SysAdminServiceImpl(RedisUtil redisUtil, ISysRoleService sysRoleService, ISysMenuService sysMenuService) {
        this.redisUtil = redisUtil;
        this.sysRoleService = sysRoleService;
        this.sysMenuService = sysMenuService;
    }

    @Override
    public AdminLoginSuccessVO login(AdminLoginDto adminLoginDto) throws UnsupportedEncodingException {
        if (!adminLoginDto.getIsAgree()) {
            throw new BusinessException("请先同意协议");
        }
        SysAdmin sysAdmin = getAdminByNameAndPassword(adminLoginDto.getName(), adminLoginDto.getPassword());
        if (ObjectUtil.isNull(sysAdmin)) {
            throw new BusinessException("用户信息不存在");
        }
        AdminLoginSuccessVO vo = new AdminLoginSuccessVO();
        vo.setSysAdmin(sysAdmin);
        String token = JwtTokenUtil.createToken(sysAdmin.getId(), sysAdmin.getName());
        vo.setToken(token);
        //管理员权限
        List<SysRole> roles = sysRoleService.selectRolesByAdminId(sysAdmin.getId());
        vo.setRoles(roles);
        //菜单
        List<SysMenu> menus = sysMenuService.selectPermsByAdminId(sysAdmin.getId());
        vo.setMenus(menus);
        redisUtil.set(RedisKeyConstant.SYS_ADMIN+sysAdmin.getId(), vo, DateUtil.offset(DateUtil.date(), DateField.DAY_OF_MONTH, TokenConstant.tokenExpireTime).getTime());
        return vo;
    }

    private SysAdmin getAdminByNameAndPassword(String name, String password) {
        return super.getOne(
                new LambdaQueryWrapper<SysAdmin>()
                        .eq(SysAdmin::getName, name)
                        .eq(SysAdmin::getPassword, DigestUtil.md5Hex(password))
        );
    }

    @Override
    public SysAdmin getAdminByName(String name) {
        SysAdmin sysAdmin = super.getOne(
                new LambdaQueryWrapper<SysAdmin>()
                        .eq(SysAdmin::getName, name)
        );
        if (ObjectUtil.isNull(sysAdmin)) {
            throw new BusinessException("用户不存在");
        }
        return sysAdmin;
    }

    @Override
    public boolean logout(Long adminId) {
        redisUtil.del(RedisKeyConstant.SYS_ADMIN+adminId);
        return true;
    }
}
